using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;

namespace FreeFlow.Client
{
  /// <summary>
  /// The type of a grid.
  /// </summary>
  public enum GridType
  {
    /// <summary>Double-clicking a row causes a refill.</summary>
    Refill,
    /// <summary>The grid is editable.</summary>
    Edit,
    /// <summary>Double-clicking a row opens another folder.</summary>
    Folder,
    /// <summary>The grid displays attachments.</summary>
    Attachment,
    /// <summary>None of these.</summary>
    None
  }

	/// <summary>
	/// A grid field.
	/// </summary>
  [Serializable]
  public class GridField : Field
  {
    internal GridField(Form form, XmlElement fieldNode)
      : base(form, fieldNode)
    {
      listDataSet = new DataSet();

      // columns
      columns = new System.Collections.Generic.List<GridColumn>();

      if (fieldNode != null)
      {
        XmlElement columnList = fieldNode["GridColumnList"];
        if (columnList != null)
        {
          foreach (XmlElement columnNode in columnList.ChildNodes)
          {
            GridColumn column = new GridColumn(columnNode);
            columns.Add(column);
          }
        }

        // grid type
        string type = fieldNode.GetAttribute("GridType");
        if (type == "Refill")
          gridType = GridType.Refill;
        else if (type == "Edit")
          gridType = GridType.Edit;
        else if (type == "Folder")
          gridType = GridType.Folder;
        else if (type == "Attachment")
          gridType = GridType.Attachment;
        else if (type == "None")
          gridType = GridType.None;
        else
          throw new FreeFlowException("Unrecognized grid type - " + type);
      }
    }

    #region columns
    private System.Collections.Generic.List<GridColumn> columns;
    /// <summary>
    /// Gets the column count for the grid.
    /// </summary>
    public int ColumnCount
    {
      get
      {
        return columns.Count;
      }
    }

    /// <summary>
    /// Gets the column at the specified index.
    /// </summary>
    /// <param name="index">Index.</param>
    public GridColumn Column(int index)
    {
      return (GridColumn)columns[index];
    }
    #endregion
    #region properties
    /// <summary>
    /// Gets the data for this grid.
    /// </summary>
    public DataTable Data
    {
      get
      {
        return listDataSet.Tables["Row"];
      }
    }

    private GridType gridType;
    /// <summary>
    /// Gets the grid type.
    /// </summary>
    public GridType GridType
    {
      get
      {
        return gridType;
      }
    }
    #endregion

    private DataSet listDataSet;
    internal override void FillValue(XmlElement node)
    {
      XmlNode dataNode = node.SelectSingleNode("Recordset/xml");
      if (dataNode != null)
      {
        string data = dataNode.OuterXml;

        StringReader Reader = new StringReader(data);
        XmlTextReader XMLReader = new XmlTextReader(Reader);
        listDataSet.Reset();
        //listDataSet.ReadXmlSchema(XMLReader);
        listDataSet.ReadXml(XMLReader);
      }
    }
	}
}
